package com.study.spring.thread;

import org.apache.storm.messaging.netty.NettyRenameThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 * @author zhang
 * @date 2019/03/22
 */
public class App {


    private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

    private static final int CORE_SIZE = 4;

    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(4);
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 3, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1), new NettyRenameThreadFactory("zhangyi"));

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < CORE_SIZE; i++) {
            poolExecutor.submit(new Processor(countDownLatch));
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            LOGGER.error(e.getMessage(), e);
            Thread.currentThread().interrupt();
        }
        LOGGER.info("完成三个线程,耗时{}毫秒", System.currentTimeMillis() - startTime);
        poolExecutor.shutdown();
    }
}
